<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * 通道黑名单模型
 * @package app\admin\model
 */
class GlobalBlack extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_black_list_all';
    //查询黑名单是否存在
    public static function getOne($data)
    {
    	$where = [
                'phone' => $data['phone']
    		];
    	return self::get($where);
    }

    //更新
    public static function upd($data,$blackId)
    {
        $data['change_time'] = date('Y-m-d H:i:s',time());
        return self::where(['id'=>$blackId])->update($data);
    }

    public static function addOne($data)
    {
        $createTime = date('Y-m-d H:i:s',time());
        $data['create_time'] = $createTime;
        $data['change_time'] = $createTime;
        return self::create($data);
    }

    //黑名单开关
    public static function openOrClose($black_id,$status)
    {
        trace(session('username').'把渠道黑名单id为'.$black_id.'的开启状态改为了'.$status,'info');
        return self::where(['id'=>$black_id])->update(['status' => $status]);
    }

    public static function addAll($data,$arr)
    {
        foreach ($arr as $val) {
            $createTime = date('Y-m-d H:i:s',time());
            $where = [
                'phone' =>$val
            ];
            $row = [
                'create_time' => $createTime,
                'change_time' => $createTime,
                'phone' =>$val,
                'mark' => $data['mark'],
            ];
            if( !self::where($where)->find() ){
                self::create($row);
            }
        }
        return true;
    }

    /**
     * [getAlls 条件查询全部数据]
     * @author   .guo
     * @DataTime 2019-05-27
     * @param    [type]     $request [条件数组]
     * @return   [type]              [object]
     */
    public static function getAlls($request)
    {
        $where = [];

        if (!empty($request['phone'])) {

            $where['b.phone'] = trim($request['phone']);
        }


        return self::where($where)
            ->paginate($request['limit'], false, $request)
            ->each(function($item, $key) {

                switch ($item->status) {
                    case '1':
                        $item->status_ = '<span class="layui-badge layui-bg-green status-qy">正常</span>';
                        break;

                    default:
                        $item->status_ = '<span class="layui-badge status-jy">禁用</span>';
                        break;
                }
            });

    }



    public static function getRow($blackId)
    {
        $where = [
                'b.id' => $blackId,
            ];
        return self::alias('b')
            ->where($where)
            ->find();
    }

}
